package com.controller;


import com.business.hexinpass.util.Validator;
import com.entity.Operator;
import com.service.MenuService;
import com.service.OperatorService;
import com.service.RoleService;
import com.vo.JsonReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.Session;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * Created by jsb-160 on 2018/7/18.
 */
@Controller
@RequestMapping("/operator")
public class OperatorController {
    @Autowired
    OperatorService operatorService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String getIndex() {
//        Integer ret=operatorService.deleteId(1);
//        System.out.println(ret+"<<<<<<");
//        Operator operator=operatorService.getOneOperator(2);
//        System.out.println(">>>>>>>>>>>>>>"+operator.toString());
        return "index";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public JsonReturn login(Operator operator, HttpSession session) {
        session.removeAttribute("logined");
        JsonReturn jsonReturn = new JsonReturn();
        try {
            if (Validator.isNull(operator.getMobile()) || Validator.isNull(operator.getPassword())) {
                jsonReturn.setSuccess(false);
                jsonReturn.setErrorCode(101);
                jsonReturn.setError("请输入用户名或密码");
                return jsonReturn;
            }
            if (Validator.isNotNull(operator.getMobile()) && Validator.isNotNull(operator.getPassword())) {
                String realPass = operatorService.getPassword(operator.getMobile());
                if (realPass == null || !realPass.equals(operator.getPassword())) {
                    jsonReturn.setSuccess(false);
                    jsonReturn.setErrorCode(102);
                    jsonReturn.setError("账号或密码不正确");
                } else {
                    Operator operator1=operatorService.getUserByMobile(operator.getMobile());
                    session.setAttribute("logined",operator1);
                    jsonReturn.setSuccess(true);
                    jsonReturn.setErrorCode(100);
                    jsonReturn.setData("登录成功");
                }

            }
        } catch (Exception e) {
            jsonReturn.setSuccess(false);
            jsonReturn.setErrorCode(108);
            jsonReturn.setData("系统错误，请稍后再试！");
            e.printStackTrace();
        }
        return jsonReturn;
    }

    /**
     * 得到所有的用户
     *
     * @return
     */
    @RequestMapping(value = "/getAllUsers")
    @ResponseBody
    public JsonReturn getAllUsers() {
        JsonReturn jsonReturn = new JsonReturn();
        List<Operator> operatorList = operatorService.getUsers();
        jsonReturn.setErrorCode(100);
        jsonReturn.setSuccess(true);
        jsonReturn.setData(operatorList);
        return jsonReturn;
    }

    /***
     * 跳转到用户界面
     * @return
     */
    @RequestMapping(value = "/returnAllUsers")
    public String returnAllUser() {
        return "userManage/list";
    }
    /***
     * 跳转到角色界面
     * @return
     */
    @RequestMapping(value = "/returnAllRoles")
    public String returnAllRoles() {
        return "roleManage/roleList";
    }
/**
 * 通过id查找一个用户
 */
@RequestMapping(value = "/getOneUserById")
@ResponseBody
public JsonReturn getOneUserById(Integer id){
    JsonReturn jsonReturn=new JsonReturn();
    try {
        if(Validator.isNull(String.valueOf(id))){
            jsonReturn.setSuccess(false);
            jsonReturn.setError("不存在该用户");
        }
        else {
           Operator operator= operatorService.getOneById(id);
            jsonReturn.setSuccess(true);
            jsonReturn.setData(operator);
        }
    } catch (Exception e) {
        jsonReturn.setSuccess(false);
        jsonReturn.setError("系统错误！");
        e.printStackTrace();
    }
    return jsonReturn;
}

    /**
     * 新增或更改用户
     */
    @RequestMapping(value = "/addDrEditUser")
    @ResponseBody
    public JsonReturn addDrEditUser(Operator operator) {
        JsonReturn jsonReturn = new JsonReturn();
        try {
            if (Validator.isNull(String.valueOf(operator.getId()))) {
                if (Validator.isNotNull(operator.getMobile()) && Validator.isNotNull(operator.getAccount())) {
                    Integer mobileLenth = operator.getMobile().length();
                    //            设置后六位为密码
                    operator.setPassword(operator.getMobile().substring(mobileLenth - 6, mobileLenth));
                    Integer id = operatorService.insertSelective(operator);
                    if (Validator.isNotNull(String.valueOf(id))) {
                        jsonReturn.setSuccess(true);
                        jsonReturn.setErrorCode(100);
                        jsonReturn.setData("添加用户成功");
                    }
                } else {
                    jsonReturn.setErrorCode(102);
                    jsonReturn.setSuccess(false);
                    jsonReturn.setError("请输入手机号或账号");
                }
            } else {
                Integer id = operatorService.updateUser(operator);
//                Integer id=operatorService.insertSelective(operator);
                if (Validator.isNotNull(String.valueOf(id))) {
                    jsonReturn.setSuccess(true);
                    jsonReturn.setErrorCode(100);
                    jsonReturn.setData("修改用户成功");
                } else {
                    jsonReturn.setErrorCode(102);
                    jsonReturn.setSuccess(false);
                    jsonReturn.setError("修改失败");
                }
            }

        } catch (Exception e) {
            jsonReturn.setErrorCode(108);
            jsonReturn.setSuccess(false);
            jsonReturn.setError("系统错误，请稍后再试");
            e.printStackTrace();
        }
        return jsonReturn;
    }


    /**
     * 删除一个用户
     */
    @RequestMapping(value = "/deleteUser")
    @ResponseBody
    public JsonReturn deleteUser(Integer id) {
        JsonReturn jsonReturn = new JsonReturn();
        try {
            operatorService.deleteId(id);
            jsonReturn.setErrorCode(100);
            jsonReturn.setSuccess(true);
            jsonReturn.setData("删除成功");
        } catch (Exception e) {
            jsonReturn.setErrorCode(108);
            jsonReturn.setSuccess(false);
            jsonReturn.setData("删除失败");

            e.printStackTrace();
        }
        return jsonReturn;
    }


    /**
     * 查询角色类型
     */
    @RequestMapping(value = "/selectRoles")
    @ResponseBody
    public JsonReturn selectRoles() {
        JsonReturn jsonReturn = new JsonReturn();

        try {
            jsonReturn.setData(roleService.listAllRoles());
            jsonReturn.setErrorCode(100);
            jsonReturn.setSuccess(true);
        } catch (Exception e) {
            jsonReturn.setError("系统错误，列表获取失败");
            jsonReturn.setErrorCode(108);
            jsonReturn.setSuccess(false);
            e.printStackTrace();
        }
        return jsonReturn;
    }
}
